package com.gimi.cloud.jimi.manage.controller.app;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.Page;
import com.gimi.cloud.jimi.common.utils.UUIDUtil;
import com.gimi.cloud.jimi.common.utils.oss.AliOSSUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.MajorAdDTO;
import com.gimi.cloud.jimi.entity.app.MajorAdEntity;
import com.gimi.cloud.jimi.manage.service.app.MajorAdService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @auther loongs
 * @date 2017/12/27  15:27
 */
@RestController
@RequestMapping(value = "majorAd")
public class MajorAdController {
    private static Logger logger = LoggerFactory.getLogger(MajorAdController.class);
    private MajorAdService majorAdService;
    @Autowired
    public MajorAdController(MajorAdService majorAdService) {
        this.majorAdService = majorAdService;
    }

    @RequestMapping(value = "/getMajorAdList")
    public void getMajorAdList(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) {
        logger.info("获得首页广告列表（分页），入参："+majorAdDTO);
        Response resp = new Response();
        Page<MajorAdDTO> list=majorAdService.getMajorAdList(majorAdDTO);
        resp.setRes_data(list);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/majorAdRelyColumnProd")
    public void majorAdRelyColumnProd(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) {
        logger.info("选择商品时候，查看商品列表，入参："+majorAdDTO);
        Response resp = new Response();
        Map<String,Object> data =majorAdService.majorAdRelyColumnProd(majorAdDTO);
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/majorAdRelyColumn")
    public void majorAdRelyColumn(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) {
        logger.info("主页广告对应的栏目，入参："+majorAdDTO);
        Response resp = new Response();
        Map<String,Object> data =majorAdService.majorAdRelyColumn(majorAdDTO);
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/addMajorAd")
    public void addMajorAd(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) {
        logger.info("添加首页广告，入参："+majorAdDTO);
        Response resp = new Response();
        boolean result=majorAdService.addMajorAd(majorAdDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.ADD_AD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/editMajorAd")
    public void editMajorAd(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) {
        logger.info("编辑首页广告，入参："+majorAdDTO);
        Response resp = new Response();
        boolean result=majorAdService.editMajorAd(majorAdDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.UPDATE_AD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/gotoEditMajorAd")
    public void gotoEditMajorAd(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) {
        logger.info("转跳到编辑首页广告界面，入参："+majorAdDTO);
        Response resp = new Response();
        MajorAdEntity adEntity=majorAdService.gotoEditMajorAd(majorAdDTO);
        resp.setRes_data(adEntity);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }


    @RequestMapping(value = "/deleteMajorAd")
    public void deleteMajorAd(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) throws MsgException {
        logger.info("删除首页广告，入参："+majorAdDTO);
        Response resp = new Response();
        boolean result=majorAdService.deleteMajorAd(majorAdDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.DELETE_AD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/underMajorAd")
    public void underMajorAd(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) throws MsgException {
        logger.info("撤下首页广告，入参："+majorAdDTO);
        Response resp = new Response();
        boolean result=majorAdService.underMajorAd(majorAdDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.UNDER_AD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/publishMajorAd")
    public void publishMajorAd(@RequestBody MajorAdDTO majorAdDTO, HttpServletResponse response) throws MsgException {
        logger.info("发布首页广告，入参："+majorAdDTO);
        Response resp = new Response();
        boolean result=majorAdService.publishMajorAd(majorAdDTO);
        if(result){
            resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        }else{
            resp.setResponseCode(ResponseCodeEnum.PUBLISH_AD_FAIL);
        }
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp),response);
    }

    @RequestMapping(value = "/uploadMajorAdFile")
    public void uploadMajorAdFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String url = "";
        StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        for(Object entry : multipartFiles.entrySet()){
            LinkedList<MultipartFile> lists = ((Map.Entry<String, LinkedList<MultipartFile>>) entry).getValue();
            for(MultipartFile file : lists){
                InputStream is = file.getInputStream();
                String uuId = UUIDUtil.get32UUID();
                String dirPath = "majorAd";
                String[] originalFilenames = file.getOriginalFilename().split("\\.");
                String fileName = uuId + "." + originalFilenames[originalFilenames.length-1];
                //测试上传时间
                long startTime1=System.currentTimeMillis();
                String result = AliOSSUtil.uploadFile(is,dirPath,fileName);
                long endTime1=System.currentTimeMillis();
                logger.info("上传主页ad图片到OSS，所用时间： "+(endTime1-startTime1)+"ms");

                long startTime2=System.currentTimeMillis();
                String tempUrl = AliOSSUtil.getFileUrl(result,new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 365 * 100));
                long endTime2=System.currentTimeMillis();
                logger.info("获得主页ad图片的url路径，所用时间： "+(endTime2-startTime2)+"ms");

                url += ","+tempUrl;
                System.out.println("==========>上传结果：" + url);
            }
        }

        Response resp = new Response();
        Map<String,String> data = new HashMap<>();
        data.put("url",url.equals("")?url:url.substring(1));
        resp.setRes_data(data);
        resp.setResponseCode(ResponseCodeEnum.SUCCESSED);
        Util.sendResponse(JsonUtil.toJson(resp), response);
    }
}
